import 'package:dustbin_sort/dao/home_dao.dart';
import 'package:dustbin_sort/model/news_model_entity.dart';
import 'package:dustbin_sort/widget/news_list.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:dustbin_sort/pages/speak_page.dart';
import 'package:dustbin_sort/util/navigator_util.dart';
import 'package:dustbin_sort/widget/loading_container.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:jwt_decode/jwt_decode.dart';
const SEARCH_BAR_DEFAULT_TEXT = '网红打卡地 景点 酒店 美食';

class HomePage extends StatefulWidget
{
  @override
  State<StatefulWidget> createState()=>_HomePageState();

}
class _HomePageState extends State<HomePage> {
	List<NewsModelResultData> news;
	bool _loading = true;
	final PageController _controller = PageController(
		initialPage: 0,
	);

	@override
  void initState() {
    // TODO: implement initState
		_handerRefresh();
    super.initState();
  }

	List<Widget> _swiper_images = <Widget>[
		Container(
			padding: EdgeInsets.all(10),
			margin: EdgeInsets.all(5),
			child: ClipRRect(
				borderRadius: BorderRadius.circular(9),
				child: new Image.asset(
					'assets/images/banner_1.jpg',
					fit: BoxFit.cover,
				),
			),
		),
		Container(
			padding: EdgeInsets.all(10),
			margin: EdgeInsets.all(5),
			child: ClipRRect(
				borderRadius: BorderRadius.circular(9),
				child: new Image.asset(
					'assets/images/banner_1.jpg',
					fit: BoxFit.cover,
				),
			),
		),

	];

	Future<Null> _handerRefresh() async{
		try{
			NewsModelEntity model = await HomeDao.fetch();
			print(model.toJson());
			setState(() {
			  news = model.result.data;
			  _loading = false;
			});
		}catch(e){
			print(e);
			setState(() {
			  _loading = false;
			});
		}
		return null;
	}

	@override
	Widget build(BuildContext context) {
		var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoibGlueXVmZWkiLCJleHAiOjE1ODM5MDYxMzR9.dkKz5GB_FmTK-27SRZs6x67tFP6QC24tMSVGGXuLeKg';
		var payload = Jwt.parseJwt(token);
		print(payload);
		return Scaffold(
			backgroundColor: Colors.white,

			appBar: AppBar(
				title: Text(
					"垃圾分类",
					style: TextStyle(color: Colors.black,fontWeight: FontWeight.w300,),
				),
				backgroundColor: Colors.white,
				centerTitle: false,
				elevation: 0,
				actions: <Widget>[
					Padding(
						padding: EdgeInsets.only(right: 9),
						child: new Icon(Icons.search,color: Colors.black,),
					),
				],
			),
			body: LoadingContainer(
				isLoading: _loading,
				child: Stack(
					children: <Widget>[
						MediaQuery.removePadding(
							removeTop: true,
							context: context,
							child: RefreshIndicator(
								onRefresh: _handerRefresh,
								child: ListView(
									children: <Widget>[
										new Container(
											color: Colors.white,
											margin: EdgeInsets.all(0),
											padding: EdgeInsets.all(0),
											height: 210,
//									width: 300,
											child: Container(
												child: new Swiper(
														itemBuilder: (BuildContext context, int index){
															return _swiper_images[index];
														},
														itemHeight: MediaQuery.of(context).size.height,
														itemWidth: MediaQuery.of(context).size.width,
														itemCount: _swiper_images.length,
														autoplay: false,
														loop: true,
														pagination: SwiperPagination(
																builder: SwiperPagination.dots
														),
														layout: SwiperLayout.CUSTOM,
														customLayoutOption: new CustomLayoutOption(
																startIndex: -1,
																stateCount: 3
														).addRotate([
															-45.0/180,
															0.0,
															45.0/180
														]).addTranslate([
															new Offset(-370.0, -40.0),
															new Offset(0.0, 0.0),
															new Offset(370.0, -40.0)
														])
												),
											),
										),

										Container(
											decoration: BoxDecoration(
												color: Colors.white,
												borderRadius: BorderRadius.circular(6),
											),
											child: Padding(

												padding: EdgeInsets.all(10),
												child: Row(
													mainAxisAlignment: MainAxisAlignment.spaceAround,
													children: <Widget>[

														Container(
															child:Column(
																children: <Widget>[
																	Expanded(child:  ClipRRect(
																		borderRadius: BorderRadius.circular(9),
																		child: new Image.asset(
																			'assets/images/ares.png',
																			fit: BoxFit.contain,
																		),
																	),flex: 4,),
																	Expanded(child: Container(
																		padding: EdgeInsets.only(top: 10),
																		child: Text("AR识别", style: TextStyle(fontWeight: FontWeight.bold),),
																	),flex: 3,)
																],
															),
															decoration: BoxDecoration(
																	borderRadius: BorderRadius.circular(6)
															),
															width: 100,
															height: 100,
														),
														Container(
															child:Column(
																children: <Widget>[
																	Expanded(child:  ClipRRect(
																		borderRadius: BorderRadius.circular(9),
																		child: new Image.asset(
																			'assets/images/camera.png',
																			fit: BoxFit.contain,
																		),
																	),flex: 4,),
																	Expanded(child: Container(
																		padding: EdgeInsets.only(top: 10),
																		child: Text("拍照识别", style: TextStyle(fontWeight: FontWeight.bold),),
																	),flex: 3,)
																],
															),
															decoration: BoxDecoration(
																	borderRadius: BorderRadius.circular(6)
															),
															width: 100,
															height: 100,
														),
														GestureDetector(
															onTap: (){
																NavigatorUtil.push(context, SpeakPage());
															},
															child: Container(
																child:Column(
																	children: <Widget>[
																		Expanded(child:  ClipRRect(
																			borderRadius: BorderRadius.circular(9),
																			child: new Image.asset(
																				'assets/images/voice.png',
																				fit: BoxFit.contain,
																			),
																		),flex: 4,),
																		Expanded(child: Container(
																			padding: EdgeInsets.only(top: 10),
																			child: Text("语音识别", style: TextStyle(fontWeight: FontWeight.bold),),
																		),flex: 3,)
																	],
																),
																decoration: BoxDecoration(
																		borderRadius: BorderRadius.circular(6)
																),
																width: 100,
																height: 100,
															),
														)

													],
												),
											),
										),
										Container(
											color: Colors.white,
											padding: EdgeInsets.fromLTRB(15, 0, 0, 0),
											child:Text("垃圾科普小知识",style: TextStyle(
												fontSize: 18,
												fontWeight: FontWeight.bold,
											),
											),
										),

										NewsList(newsModelResult: news)
									],
								),
							),
						)

					],
				),
			)
		);
	}

}